<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 会员充值接口
 */
class Controller_Pc_Api_V2_Wallet extends Controller_Pc_Api_v2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Wallet
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Wallet
     */
    private $service;

    public function before()
    {
        parent::before();

        // 验证
        $this->validation = Request_Api_V2_Factory::create('wallet', $this->request_params);

        // 服务
        $this->service = new Service_Api_Wallet();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    /**
     * @return void
     * 获取可充值的面额
     */
    public function action_get_recharge_amount()
    {
        try {
            $res = $this->service->search();

            $this->result['data'] = $res;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    /**
     * @return void
     * 充值下单
     */
    public function action_add_order()
    {
        try {
            $this->validation->order()->check();

            $this->request_params['member_info'] = $this->member_info;
            $res = $this->service->create_order($this->request_params);

            $this->result['msg']  = '订单创建成功';
            $this->result['data'] = $res;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    /**
     * @return void
     * @throws Kohana_Exception
     * 产品订单抵扣
     */
    public function action_product_deduction()
    {
        $db = Database::instance();
        $db->begin();
        try {
            $this->validation->product_deduction()->check();

            $res = $this->service->product_deduction_handle($this->request_params);

            $this->result['msg']  = '订单抵扣成功';
            $this->result['data'] = $res;
            $db->commit();
        } catch (Exception $e) {
            $db->rollback();
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    /**
     * @return void
     * 获取筛选条件
     */
    public function action_query_condition()
    {
        try {
            $res = $this->service->query_condition();

            $this->result['data'] = $res;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    /**
     * @return void
     * 余额明细
     */
    public function action_wallet_details_list()
    {
        try {
            $this->validation->wallet_details_list()->check();

            $res = $this->service->wallet_details_list($this->request_params);

            $this->result['data'] = $res;
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }
}